package com.hcy.app.screen.miao

import android.Manifest
import android.content.Context
import androidx.annotation.RequiresPermission
import androidx.lifecycle.ViewModel
import com.hcy.app.utils.AppInfo
import com.hcy.app.utils.DeviceInfo
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import javax.inject.Inject

@HiltViewModel
class IDPViewModel @Inject constructor():ViewModel(){

    private val _idpInfoState = MutableStateFlow(IdpInfo())
    val idpInfoState: StateFlow<IdpInfo> = _idpInfoState.asStateFlow()

    private val _idpConfState = MutableStateFlow(IdpConf())
    val idpConfState: StateFlow<IdpConf> = _idpConfState.asStateFlow()


    init {

    }

    private inline fun updateInfoState(transform: IdpInfo.() -> IdpInfo){
        _idpInfoState.update { state -> state.transform() }
    }

    private inline fun updateConfState(transform: IdpConf.() -> IdpConf){
        _idpConfState.update { state -> state.transform() }
    }

    fun updateConfSerAddr(serAddr: String){
        updateConfState { copy( serAddr= serAddr ) }
    }
    fun updateConfDialog(isShow: Boolean){
        updateConfState { copy( showConfDialog = isShow ) }
    }

    @RequiresPermission(allOf = [Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION])
    fun updateIdpInfo(ctx:Context){
        updateInfoState{ copy( imei = DeviceInfo.getIMEI(ctx) ) }
        updateInfoState{ copy( androidVersion = DeviceInfo.getAndroidVersion() ) }
        updateInfoState{ copy( brand = DeviceInfo.getBrand() ) }
        updateInfoState{ copy( model = DeviceInfo.getModel() ) }
        updateInfoState{ copy( cpuInfo = DeviceInfo.getCpuInfo() ) }
        updateInfoState{ copy( gps = DeviceInfo.getGPSInfo(ctx) ) }
        updateInfoState{ copy( ip = DeviceInfo.getIPAddress(ctx) ) }
        updateInfoState{ copy( screenInfo = DeviceInfo.getScreenInfo(ctx) ) }
        updateInfoState{ copy( memory = DeviceInfo.getMemoryInfo() ) }
        updateInfoState{ copy( storageInfo = DeviceInfo.getStorageInfo() ) }
        updateInfoState{ copy( appList = DeviceInfo.getInstalledApps(ctx) ) }
        updateInfoState{ copy( isRoot = DeviceInfo.isDeviceRooted() ) }
        updateInfoState{ copy( isUsbDebug = DeviceInfo.isUsbDebuggingEnabled(ctx) ) }
        updateInfoState{ copy( openVpn = DeviceInfo.isVpnActive(ctx) ) }
    }

}

data class IdpInfo(
    val imei: String = "", // IMEI
    val androidVersion:String="", // android 版本
    val brand: String="", // 品牌
    val model:String="", // 型号
    val cpuInfo:String="",// CPU 信息、核心数、架构
    val gps: String = "" , // 位置信息
    val ip: String = "" , // ip 信息
    // 屏幕分辨率、DPI
    val screenInfo : String = "", // 屏幕信息
    val memory: String = "",// 内存信息
    val storageInfo: String = "" , // 存储空间
    val appList:List<AppInfo> = emptyList<AppInfo>() , // 软件信息
    // 安全性
    val isRoot : Boolean = false, // 是否开启了root 权限
    val isUsbDebug : Boolean = false, // 是否开启USB调试
    val openVpn : Boolean = false, // 是否启用了 vpn
)

data class IdpConf(
    val serAddr: String = "http://123.57.147.147:19005",
    val showConfDialog: Boolean = false,
)
